package LearnDataStructure.b_散列结构.HashSet;

import LearnDataStructure.b_散列结构.HashMap.MyHashMap;
import LearnDataStructure.b_散列结构.MyHashSetAPI;
import LearnDataStructure.b_散列结构.useNode;

import java.util.Iterator;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-05 19:16
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.b_散列结构.HashSet
 */
public class MyHashSet<K> implements MyHashSetAPI<K> {
    private MyHashMap<K,K> map = new MyHashMap<>();
    @Override
    public void add(K key) {
        map.put(key,null);
    }

    @Override
    public boolean contains(K key) {
        return map.containsKey(key);
    }

    @Override
    public void remove(K key) {
        map.remove(key);
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public void clear() {
        map.clear();
    }

    @Override
    public Iterator<K> iterator() {
        Iterator<useNode<K, K>> iterator = map.iterator();

        return new Iterator<K>() {
            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @Override
            public K next() {
                return iterator.next().key;
            }
        };
    }

    @Override
    public String toString() {
        Iterator<useNode<K, K>> iterator = map.iterator();
        StringBuilder sb = new StringBuilder();
        while (iterator.hasNext()) {
            sb.append(iterator.next().key + ",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append(
                "; capacity = " + map.getCapacity() +
                ", size = " + map.size()
        );
        return sb.toString();
    }
}
